import { createRouter, createWebHashHistory } from "vue-router";
import {
  Setting,
  Van,
  ShoppingBag,
  Notification,
  Opportunity,
  Monitor,
  Collection,
  Tickets,
  Money,
  Cloudy,
  Postcard,
} from "@element-plus/icons-vue";
import { getUserInfo } from "@/api/login.js";
function getToken() {
  return sessionStorage.getItem("loginToken");
}
function setToken(data) {
  return sessionStorage.setItem("loginToken", data);
}
export const routes = [
  {
    path: "/redirectZW",
    name: "redirect",
    hidden: true,
    component: () => import("@/views/redirect.vue"),
    beforeEnter: (to, from, next) => {
      if (to.query.loginToken) {
        setToken(to.query.loginToken);
        getInfo(to.query.loginToken).then((res) => {
          next("/rzsq/shenbao");
        });
      } else {
        next("/login");
      }
    },
  },
  {
    path: "/redirect",
    name: "redirect",
    hidden: true,
    component: () => import("@/views/redirect.vue"),
    beforeEnter: (to, from, next) => {
      if (to.query.loginToken) {
        setToken(to.query.loginToken);
        getInfo(to.query.loginToken).then((res) => {
          next("/rzsq/shenbao");
        });
      } else {
        next("/login");
      }
    },
  },
  {
    path: "/login",
    name: "login",
    hidden: true,
    component: () => import("@/views/login.vue"),
  },
  {
    hidden: true,
    path: "/forgotPass",
    component: () => import("@/views/forgotPass.vue"),
  },
  {
    hidden: true,
    path: "/",
    redirect: "/hdgl",
    component: () => import("@/views/Layout.vue"),
    meta: {},
    children: [
      {
        path: "/hdgl",
        name: "hdgl",
        meta: {
          title: "活动管理",
          icon: Tickets,
        },
        component: () => import("@/views/hdgl/hdgllist.vue"),
      },
      {
        path: "/spgl",
        name: "spgl",
        meta: {
          title: "视频管理",
          icon: Tickets,
        },
        component: () => import("@/views/spgl/spgllist.vue"),
      },
      {
        path: "/listNewStanTechOrg",
        name: "listNewStanTechOrg​",
        meta: {
          title: "新型标准化技术组织",
          icon: Tickets,
        },
        component: () => import("@/views/listNewStanTechOrg​/index.vue"),
      },
      {
        path: "/zldt",
        name: "zldt",
        meta: {
          title: "质量地图",
          icon: Tickets,
        },
        children: [
          {
            path: "/zfzlj",
            name: "zfzlj",
            meta: {
              title: "政府质量奖(荣誉)",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/zfzlj/index.vue"),
          },
          {
            path: "/rzcyjg",
            name: "rzcyjg",
            meta: {
              title: "认证从业机构",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/rzcyjg/index.vue"),
          },
          {
            path: "/zljcssjcfwxm",
            name: "zljcssjcfwxm",
            meta: {
              title: "质量基础设施集成服务项目",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/zljcssjcfwxm/index.vue"),
          },
          {
            path: "/cyjlcszx",
            name: "cyjlcszx",
            meta: {
              title: "产业计量测试中心",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/cyjlcszx/index.vue"),
          },
          {
            path: "/gjjlsjjsyyjd",
            name: "gjjlsjjsyyjd",
            meta: {
              title: "国家计量数据建设应用基地",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/gjjlsjjsyyjd/index.vue"),
          },
          {
            path: "/fdjljcjg",
            name: "fdjljcjg",
            meta: {
              title: "法定计量检定机构",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/fdjljcjg/index.vue"),
          },
          {
            path: "/cxjlsf",
            name: "cxjlsf",
            meta: {
              title: "诚信计量示范街镇（乡）（荣誉）",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/cxjlsf/index.vue"),
          },
          {
            path: "/zybzhjswyh",
            name: "zybzhjswyh",
            meta: {
              title: "上海市专业标准化技术委员会表",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/zybzhjswyh/index.vue"),
          },
          {
            path: "/gjzjzx",
            name: "gjzjzx",
            meta: {
              title: "国家质检中心",
              icon: Tickets,
            },
            component: () => import("@/views/zldt/gjzjzx/index.vue"),
          },
        ],
      },
      {
        path: "/yygl",
        name: "yygl",
        meta: {
          title: "应用管理",
          icon: Tickets,
        },
        component: () => import("@/views/yygl/yygllist.vue"),
      },
      {
        path: "/jsgl",
        name: "jsgl",
        meta: {
          title: "角色管理",
          icon: Tickets,
        },
        component: () => import("@/views/jsgl/jsgllist.vue"),
      },
      {
        path: "/zzgl",
        name: "zzgl",
        meta: {
          title: "组织管理",
          icon: Tickets,
        },
        component: () => import("@/views/zzgl/zzgllist.vue"),
      },
      {
        path: "/yhgl",
        name: "yhgl",
        meta: {
          title: "用户管理",
          icon: Tickets,
        },
        component: () => import("@/views/yhgl/yhgllist.vue"),
      },
      {
        path: "/grqxgl",
        name: "grqxgl",
        meta: {
          title: "个人权限管理",
          icon: Tickets,
        },
        component: () => import("@/views/qxgl/grqxgllist.vue"),
      },
      {
        path: "/zzqxgl",
        name: "zzqxgl",
        meta: {
          title: "组织权限管理",
          icon: Tickets,
        },
        component: () => import("@/views/qxgl/zzqxgllist.vue"),
      },
      // {
      //   path: "/rzsq",
      //   name: "rzsq",
      //   meta: {
      //     title: "认证申请",
      //     icon: Monitor,
      //   },
      //   children: [
      //     {
      //       path: "/rzsq/shenbao",
      //       name: "shenbao",
      //       meta: {
      //         title: "申报管理",
      //         icon: Van,
      //       },
      //       component: () => import("@/views/rzsqlist/shenbao.vue")
      //     },
      //     {
      //       path: "/rzsq/detail",
      //       name: "rzsqDetail",
      //       hidden: true,
      //       component: () => import("@/views/rzsqlist/detail.vue"),
      //     },
      //     {
      //       path: "/rzsq/addUnit",
      //       name: "addUnit",
      //       hidden: true,
      //       component: () => import("@/views/rzsqlist/addUnit.vue"),
      //     },
      //     {
      //       path: "/rzsq/apply",
      //       name: "rzsqApply",
      //       hidden: true,
      //       component: () => import("@/views/rzsqlist/apply.vue"),
      //     },
      //     {
      //       path: "/rzsq/shenpi",
      //       name: "shenpi",
      //       meta: {
      //         title: "审批管理",
      //         icon: "location",
      //       },
      //       component: () => import("@/views/rzsqlist/shenpi.vue"),
      //     },
      //     {
      //       path: "/rzsq/anfang",
      //       name: "anfang",
      //       meta: {
      //         title: "暗访管理",
      //         icon: "location",
      //       },
      //       component: () => import("@/views/rzsqlist/anfang.vue"),
      //     },
      //     // 暗访结果录入
      //     {
      //       path: "/rzsq/anfang/apply",
      //       name: "anfangApply",
      //       hidden: true,
      //       component: () => import("@/views/rzsqlist/anfangApply.vue"),
      //     },
      //     {
      //       path: "/rzsq/yanshou",
      //       name: "yanshou",
      //       meta: {
      //         title: "验收管理",
      //         icon: "location",
      //       },
      //       component: () => import("@/views/rzsqlist/yanshou.vue"),
      //     },
      //   ],
      // },
      // {
      //   path: "/company/detail",
      //   name: "companyDetail",
      //   hidden: true,
      //   component: () => import("@/views/companyList/detail.vue"),
      // },
      // {
      //   path: "/association",
      //   name: "association",
      //   meta: {
      //     title: "行业协会管理",
      //     icon: Money,
      //   },
      //   component: () => import("@/views/associationList/index.vue"),
      // },
      // {
      //   path: "/expert",
      //   name: "expert",
      //   meta: {
      //     title: "专家库管理",
      //     icon: Postcard,
      //   },
      //   component: () => import("@/views/expertList/index.vue"),
      // },
      // {
      //   path: "/statistics",
      //   name: "statistics",
      //   meta: {
      //     title: "统计查询",
      //     icon: Opportunity,
      //   },
      //   component: () => import("@/views/statistics/index.vue"),
      // },
      // {
      //   path: "/xtgl",
      //   name: "xtgl",
      //   meta: {
      //     title: "系统管理",
      //     icon: Setting,
      //   },
      //   children: [
      //     {
      //       path: "/qx",
      //       name: "qx",
      //       meta: {
      //         title: "权限管理",
      //         icon: "location",
      //       },
      //       component: () => import("@/views/system/qx.vue"),
      //     },
      //     {
      //       path: "/notify",
      //       name: "notify",
      //       meta: {
      //         title: "通知管理",
      //         icon: Notification,
      //       },
      //       component: () => import("@/views/system/notify.vue"),
      //     }
      //   ],
      // }
    ],
  },
];
const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_APP_PUBLIC_PATH),
  routes: routes,
});

const whiteList = ["/login", "/404", "/401", "/forgotPass"];
router.beforeEach((to, from, next) => {
  if (getToken()) {
    if (to.query.loginToken && getToken() !== to.query.loginToken) {
      setToken(to.query.loginToken);
      getInfo(to.query.loginToken).then((res) => {
        next();
      });
    } else {
      next();
    }
  } else {
    if (to.query.loginToken) {
      setToken(to.query.loginToken);
      getInfo(to.query.loginToken).then((res) => {
        next(to.path);
      });
    } else {
      next();
      // if(to.path === '/login'){
      //   next()
      // }else{
      //   next(`/login`)
      // }
    }
  }
});
// 通过loginToken获取用户信息
function getInfo(loginToken) {
  return new Promise((resolve, reject) => {
    if (loginToken) {
      let temp = {
        userName: "管理员",
      };
      sessionStorage.setItem("userInfo", JSON.stringify(temp));
      resolve(temp);
    }
    // getUserInfo({
    //   loginToken: loginToken,
    // })
    //   .then((res) => {
    //     sessionStorage.setItem("userInfo", JSON.stringify(res));
    //     resolve(res);
    //   })
    //   .catch((err) => {
    //     reject(err);
    //   });
  });
}
export default router;
